/*
 * Copyright (c) 2009, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @addtogroup runtime_control
 * @{
 */
/**
 * @file 
 * Implementation of the local runtime control functionality
 * @author 
 * Wouter Horré <wouter.horre@cs.kuleuven.be>
 */

#include <string.h>

#include "runtime_control_local.h"
#include "components/components_private.h"
#include "events/events_private.h"

#ifdef LOOCI_RC_LOCAL_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF printf
#endif // CONTIKI_TARGET_AVR_RAVEN
#else
#define PRINTF(...)
#endif

#define WIRE_ANY 0
#define WIRE_ALL 1 

bool looci_rc_deactivate(u8_t cid) {
  struct component * c = components_get(cid);
  if(c!=NULL && c->state == COMPONENT_STATE_ACTIVE) {
    components_stop(cid);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_activate(u8_t cid) {
  struct component * c = components_get(cid);
  if(c!=NULL && c->state == COMPONENT_STATE_DEACTIVATED) {
    components_start(cid);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_reset_wirings(u8_t cid) {
  struct component * c = components_get(cid);
  if(c!=NULL) {
    events_unwire_component(cid);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_wire_local(looci_eventtype_t type, u8_t sid, u8_t tid) {
  struct component * source = components_get(sid);
  struct component * target = components_get(tid);
  if(source != NULL && target != NULL) {
    events_add_local_subscription(type, source, target);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_wire_remote_from(looci_eventtype_t type, peer_id_t snode, u8_t sid, u8_t tid) {
  PRINTF("Wiring event type %u from remote node %u, sid %u, tid %u\r\n", type, snode, sid, tid);
  struct component * destination = components_get(tid);
  if(destination != NULL) {
    events_add_remote_subscription_from(type, snode, sid, destination);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_wire_remote_from_all(looci_eventtype_t type, u8_t tid) {
  return looci_rc_wire_remote_from(type, PEER_ID_NONE, COMPONENT_ID_NONE, tid);
}

bool looci_rc_wire_remote_to(looci_eventtype_t type, u8_t sid, peer_id_t tnode) {
  PRINTF("Wiring event type %u, sid %u to remote node %u\r\n", type, sid, tnode);
  struct component * source = components_get(sid);
  if(source != NULL) {
    events_add_remote_subscription_to(type, source, tnode);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_wire_remote_to_all(looci_eventtype_t type, u8_t sid) {
  return looci_rc_wire_remote_to(type, sid, PEER_ID_ALL);
}

bool looci_rc_unwire_local(looci_eventtype_t type, u8_t sid, u8_t tid) {
  PRINTF("Unwiring event type %u from id %u to id %u\r\n", type, sid, tid);
  struct component * source = components_get(sid);
  struct component * target = components_get(tid);
  if(source != NULL && target != NULL) {
    events_remove_local_subscription(type, source, target);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_unwire_remote_from(looci_eventtype_t type, peer_id_t snode, u8_t sid, u8_t tid) {
  PRINTF("Unwiring event type %u from remote node %u, sid %u, tid %u\r\n", type, snode, sid, tid);
  struct component * destination = components_get(tid);
  if(destination != NULL) {
    events_remove_remote_subscription_from(type, snode, sid, destination);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_unwire_remote_from_all(looci_eventtype_t type, u8_t tid) {
  return looci_rc_unwire_remote_from(type, PEER_ID_NONE, COMPONENT_ID_NONE, tid);
}

bool looci_rc_unwire_remote_to(looci_eventtype_t type, u8_t sid, peer_id_t tnode) {
  PRINTF("Unwiring event type %u, sid %u to remote node %u\r\n", type, sid, tnode);
  struct component * source = components_get(sid);
  if(source != NULL) {
    events_remove_remote_subscription_to(type, source, tnode);
    return true;
  } else {
    return false;
  }
}

bool looci_rc_unwire_remote_to_all(looci_eventtype_t type, u8_t sid) {
  return looci_rc_unwire_remote_to(type, sid, PEER_ID_ALL);
}

/** @} */
